package com.example.hfut_edu;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class page_loadurl extends AppCompatActivity {

    public static page_loadurl loadurl_obj = null;

    WebView webView_load;

    @SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_loadurl);
        loadurl_obj = this;

        Intent i = getIntent();  //直接获取传过来的intent
        String tempstr= i.getStringExtra("extra_data");


        webView_load = (WebView) findViewById(R.id.loadurl_webview);
        WebSettings webSettings = webView_load.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView_load.getSettings().setDomStorageEnabled(true);
        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
        webView_load.getSettings().setAppCachePath(appCachePath);
        webView_load.getSettings().setAllowFileAccess(true);
        webView_load.getSettings().setAppCacheEnabled(true);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
            webView_load.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }


        //将一个java类对象传递到HTML页面中，
        webView_load.addJavascriptInterface(this, "JSBridge");
        webView_load.setWebChromeClient(new WebChromeClient());
        Toast.makeText(this, tempstr, Toast.LENGTH_SHORT).show();
        webView_load.loadUrl(tempstr);
        webView_load.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        webView_load.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                //设定加载结束的操作
                String url0="javascript:var oHead = document.getElementsByTagName('HEAD').item(0);var oScript= document.createElement('script');oScript.type = 'text/javascript';oScript.src='";
                String url1_jiaowu="https://iou.ink/file/api/jiaowu.js";
                String url1_xxmh="https://iou.ink/file/api/xxmh.js";
                String url1_yikatong="https://iou.ink/file/api/ykt.js";
                String url2="';oHead.appendChild( oScript);;";
                if(tempstr.contains("cas.hfut.edu.cn")){
                    String tempurl=url0+url1_xxmh+url2;
                    webView_load.loadUrl(tempurl);
                }else if(tempstr.contains("jxglstu.hfut.edu.cn")){
                    String tempurl=url0+url1_jiaowu+url2;
                    webView_load.loadUrl(tempurl);

                }else if(tempstr.contains("155.94.158.151")){
                    String tempurl=url0+url1_yikatong+url2;
                    webView_load.loadUrl(tempurl);
                }
            }
        });

    }


    @JavascriptInterface
    public void toaststh(final String contentStr) {//js->原生
        //Log.d("TAG", "contentStr:" + contentStr);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(page_loadurl.this, "hello", Toast.LENGTH_SHORT).show();
            }
        });
    }
    @JavascriptInterface
    public void getaccountdata(final String contentStr) {//js->原生
        //Log.d("TAG", "contentStr:" + contentStr);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (contentStr.contains("jiaowu")){
                    String jw_account= rwSPdata("r","jw_account","");
                    String jw_passwd= rwSPdata("r","jw_passwd","");
                    webView_load.loadUrl("javascript:autofill('"+jw_account+"','"+jw_passwd+"')");
                }
                if (contentStr.contains("xxmh")){
                    String xxmh_account= rwSPdata("r","xxmh_account","");
                    String xxmh_passwd= rwSPdata("r","xxmh_passwd","");
                    webView_load.loadUrl("javascript:autofill('"+xxmh_account+"','"+xxmh_passwd+"')");
                }
            }
        });
    }



    public String rwSPdata(String flag,String key,String value){
        //email,passwd,vrcode
        SharedPreferences testSP= getSharedPreferences("testSP", Activity.MODE_PRIVATE);
        switch (flag){
            case "w":
                SharedPreferences.Editor editor = testSP.edit();
                editor.putString( key, value );
                editor.apply();
                return "";
            case "r":
                return testSP.getString( key, "" );
            default:
                return "";
        }
    }



}